<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/14
 * Time: 14:40
 */

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use EasyWeChat\Factory;


header('Access-Control-Allow-Origin:*');

class VideoList extends Controller

{
    //广告设置-通用设置接口
    public function advertList(){
        $key = env('APP_SHORT_NAME').'_video_advert_set';
        $list = Cache::get($key);

        if(!$list){
            $list['advert'] = DB::table('zm_advert_list')
                ->select(['id', 'path_type', 'advert_state', 'interval_num', 'screen_state'])
                ->get()
                ->toArray();
            $list['set'] = DB::table('zm_common_set')
                ->select(['dy_code', 'dy_code_state', 'recom_reward_state'])
                ->first();
            Cache::put($key,$list,60*60);
        }
        $list['set']-> share_incentive_set = mt_rand(0, 1);
        if($list){
            return $this->json('请求成功！',$list,1,200);
        }else{
            return $this->json('暂无分类数据！','',0,404);
        }
    }

    //首页接口
    public function videoList(){

        if(!isset($_POST['page'])){
            return $this->json('参数错误！','',0,404);
        }
        $key = env('APP_SHORT_NAME').'_video_list_'.$_POST['page'];
        $data = Cache::get($key);
        if(!$data){
            $data = DB::table('zm_video_list')
                ->where('show_state', 1)
                ->orderBy('updated_at','desc')
                ->select('id','title','img_url','share_img')
                ->paginate(10)
                ->toArray();
            Cache::put($key,$data,60);
        }
        $list['current_page'] = $data['current_page'];
        $list['last_page'] = $data['last_page'];
        $list['data'] = $data['data'];
        shuffle($list['data']);
        return $this->json('请求成功！',$list,1,200);
    }

    //视频详情接口
    public function videoInfo(){
        if(!isset($_POST['video_id']) || !$_POST['video_id']){
            return $this->json('参数错误！','',0,404);
        }
        //点击量累加
        DB::table('zm_video_list')->where('id',$_POST['video_id'])->increment('view_num');
        $key = env('APP_SHORT_NAME').'_video_info_'.$_POST['video_id'];
        $info = Cache::get($key);
        if(!$info){
            $info = DB::table('zm_video_list')
                ->where('id',$_POST['video_id'])
                ->select('id','title','img_url','share_img','video_url','video_time','view_num','advert_state', 'is_reward')
                ->first();
            Cache::put($key,$info,5*60);
        }
        $info->view_num ++;
        $info->is_collect = 0;
        $info->collect_num = DB::table('zm_video_collect')->where('video_id', $_POST['video_id'])->count();
        if ($info->collect_num >= 10000) {
            $info->collect_num = number_format($info->collect_num/10000,1).'W';
        }
        if(isset($_POST['user_id']) && $_POST['user_id']){
            $info->is_collect = DB::table('zm_video_collect')->where(['user_id'=>$_POST['user_id'],'video_id'=>$_POST['video_id']])->exists() ? 1 : 0;
        }
        if($info){
            return $this->json('请求成功！',$info,1,200);
        }else{
            return $this->json('该视频已被删除！','',0,404);
        }

    }

    //批量发布
    public function release(){
        if (!isset($_POST['ids'])){
            return ['code'=>301,'msg'=>'There is no ids','data'=>''];
        }
        $ids = $_POST['ids'];
        $ids = explode(',',$ids);
        $update_count = MP::whereIn('id',$ids)->where(['release_open'=>0])->update(['release_open'=>1,'release_at'=>date('Y-m-d H:i:s')]);
        return [
            'code'=>200,
            'msg'=>$update_count.'篇文章发布成功',
            'data'=>''
        ];
    }

}
